{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 文心一言"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2  \n",
    "import numpy as np  \n",
    "  \n",
    "# 假设tensor是一个PyTorch tensor  \n",
    "tensor = torch.randn((1, 480, 640)) \n",
    "  \n",
    "# 将Tensor转换为numpy数组，并调整形状（如果需要）  ssssssssssss\n",
    "image_np = tensor.numpy().transpose((1, 2, 0))  \n",
    "  \n",
    "# OpenCV默认使用BGR颜色通道顺序，如果tensor是RGB，需要转换  \n",
    "image_np = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)  \n",
    "  \n",
    "# 显示图像  \n",
    "cv2.imshow('Image', image_np)  \n",
    "cv2.waitKey(0)  # 等待按键  \n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## GPT\n",
    "\n",
    ">待调试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import cv2\n",
    "import numpy as np\n",
    "\n",
    "from torchvision import datasets\n",
    "from torchvision.utils import make_grid\n",
    "\n",
    "def tensor_to_image(tensor):\n",
    "    # 将张量转换为 numpy 数组\n",
    "    tensor = tensor.cpu().clone()  # 确保张量在 CPU 上\n",
    "    tensor = tensor.squeeze(0)     # 去掉批次维度\n",
    "    tensor = transforms.ToPILImage()(tensor)  # 转换为 PIL 图像\n",
    "    return cv2.cvtColor(np.array(tensor), cv2.COLOR_RGB2BGR)  # 转换为 OpenCV 格式（BGR）\n",
    "\n",
    "\n",
    "# 将批次中的第一个张量转换为图片\n",
    "image = torch.randn((1, 480, 640))\n",
    "cv2_image = tensor_to_image(image)\n",
    "\n",
    "# 使用 OpenCV 显示图像\n",
    "cv2.imshow('Image', cv2_image)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
